package com.tjp.eight.ui.fragment.otherhomepage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.comment.AppManager;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.CircleImageView;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

import org.w3c.dom.Text;

/**
 * Created by Administrator on 2017/2/24.
 */

public class FragmentOtherHomePage extends BaseFragment<OtherHomePagePresenter> implements OtherHomePageView {
    private RecyclerView recyclerView;
    private OtherHomePageAdapter otherHomePageAdapter;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private AppBarLayout appbar;
    private RelativeLayout rl_alpha_bg;
    private RelativeLayout rl_back;
    private TextView tv_name;
    private TextView tv_tips;
    private CircleImageView civ_home_pic_u;
    private String uid = "";
    private int page = 1;

    @Override
    protected OtherHomePagePresenter createPresenter() {
        return new OtherHomePagePresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.other_home_page, container, false);
        }
        initViews();
        uid = getActivity().getIntent().getStringExtra("uid");
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }


    private String getPJson() {
        OtherHomePModel model = new OtherHomePModel();
        model.uid = uid;
        model.page = page;
        page += 1;
        return new Gson().toJson(model);
    }

    private void initViews() {
        tv_name = (TextView) contentView.findViewById(R.id.tv_name);
        tv_tips = (TextView) contentView.findViewById(R.id.tv_tips);
        civ_home_pic_u = (CircleImageView) contentView.findViewById(R.id.civ_home_pic_u);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.rv_other_home);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        otherHomePageAdapter = new OtherHomePageAdapter(getActivity());
        otherHomePageAdapter.setNameOnClick(new OtherHomePageAdapter.NameOnClick() {
            @Override
            public void onClick(String name) {
                //Toast.makeText(getContext(), name + "", Toast.LENGTH_SHORT).show();
            }
        });
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(otherHomePageAdapter);
        recyclerView.setAdapter(headerViewRecyclerAdapter);

        appbar = (AppBarLayout) contentView.findViewById(R.id.appbar);
        rl_alpha_bg = (RelativeLayout) contentView.findViewById(R.id.rl_alpha_bg);
        rl_back = (RelativeLayout) contentView.findViewById(R.id.rl_back);
        recyclerView.addOnScrollListener(
                new EndlessRecyclerOnScrollListener(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)) {
                    @Override
                    public void onLoadMore(int currentPage) {
                        presenter.loadData(getPJson());
                    }
                });
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                float offset = Math.abs(verticalOffset);
                float range = appbar.getTotalScrollRange();
                float alpha = offset / range;
                rl_alpha_bg.setAlpha(alpha);
            }
        });
        rl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(getActivity());
            }
        });
    }

    @Override
    public void loadSuccess(OtherHomeRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.data != null && model.data.listdata != null && !model.data.listdata.isEmpty()) {
                    otherHomePageAdapter.addData(model.data.listdata);
                }
                tv_name.setText("" + model.name);
                tv_tips.setText("" + model.view);
                Glide.with(getActivity()).load(model.iconurl).error(R.drawable.icon_login).into(civ_home_pic_u);
            } else {
                ToatUtils.toastSuccess(getContext(), model.msg);
            }
        }
    }

    @Override
    public void fail(String msg) {

    }
}
